package com.nakamachizu.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.StringUtil;
import org.slim3.util.TimeZoneLocator;

import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.nakamachizu.constant.AppConstant;
import com.nakamachizu.model.LandMarkData;
import com.nakamachizu.model.MapData;
import com.nakamachizu.service.MapDataService;
import com.nakamachizu.util.ErrorsUtil;

public class AddController extends Controller {

    private String keyStr;
    private String name;
    private String comment;
    private String icon;
    private String lat;
    private String lng;
    
    @Override
    public Navigation run() {
        
        init();
        
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        
        if(StringUtil.isEmpty(keyStr)){
            return redirect("/make");
        }
        
        MapDataService service = new MapDataService();
        MapData map = service.getMapDataAccessKey(keyStr);
        
        if(map != null && map.getAdminMailAddress().equals(user.getEmail())){
            List<LandMarkData> list = map.getLandMarkDataListRef().getModelList();
            if(list == null){
                list = new ArrayList<LandMarkData>();
            }
            if(list.size() < AppConstant.MAX_LANDMARK_NUM()){
                Calendar now = Calendar.getInstance(TimeZoneLocator.get());
                LandMarkData lmd = new LandMarkData();

                GeoPt pt = new GeoPt(Float.parseFloat(lat), Float.parseFloat(lng));
                
                lmd.setName(name);
                lmd.setComment(comment);
                lmd.setLatLng(pt);
                lmd.setPcIconName(icon);
                lmd.setMIconName(service.createMobileLandmarkIconName(list));
                lmd.setEntryDate(now.getTime());
                lmd.setModifyDate(now.getTime());
                lmd.getMapDataRef().setModel(map);
                Calendar expire = Calendar.getInstance(TimeZoneLocator.get());
                expire.set(2050, 12, 31);
                lmd.setExpireDate(expire.getTime());
                list.add(lmd);
                
                Datastore.put(lmd);
            }else{
                ErrorsUtil.putErrors(errors, request, "message.error.maxlandmark", "landmark_over");
            }
        }
        
        return redirect("/view");
    }
    
    private void init(){
        keyStr = requestScope("key");
        name = requestScope("name");
        comment = requestScope("comment");
        icon = requestScope("ico");
        lat = requestScope("lat");
        lng = requestScope("lng");
    }
}
